<?php
/* 
 * 
 * 
 */
class UserController extends Controller
{
 
	public $img;
    public function actionUserpage()
    {
       $user= new User();
        if (isset($_GET['id']))
        {
           $Info=user::model()->with('user_info')->findByPk($_GET['id']);

          
            $friend = User::model()->with('user_friend')->findAll('user_id_f=:user_id_f',array(':user_id_f'=>$_GET['id']));
            $flag_mypage=true;

        }
        else
        {
             $Info=user::model()->with('user_info')->findByPk(Yii::app()->user->getId());
             $friend = User::model()->with('user_friend')->findAll('user_id_f=:user_id_f',array(':user_id_f'=>Yii::app()->user->getId()));
             $flag_mypage=false;
        }

        $this->render('mypage',array('info'=>$Info,'friends'=>$friend,'flag_mypage'=>$flag_mypage));
    }

    function actionRatio()
    {

        $Info=User::model()->with('user_info')->findAll();
        $this->render('ratio',array('user'=>$Info));
    }

	public function actionUpdate()
	{
		if ( (isset($_POST['id'])) && (isset($_POST['name'])) && (isset($_POST['lastname'])) && (isset($_POST['email']))&&(isset($_POST['icq'])) &&(isset($_POST['skype'])) &&(isset($_POST['jabber'])) &&(isset($_POST['site'])) &&(isset($_POST['phone'])))
		{
			$info=user_info::model()->findAll('user_id=:user_id',array(':user_id'=>$_POST['id']));
			//$email_info=User::model()->findAll('user_id=:user_id',array(':user_id'=>$_POST['id']));
			$info[0]['name']=$_POST['name'];
			$info[0]['famely']=$_POST['lastname'];
			$info[0]['icq']=$_POST['icq'];
			$info[0]['skype']=$_POST['skype'];
			$info[0]['jabber']=$_POST['jabber'];
			$info[0]['phone']=$_POST['phone'];
			$info[0]['site']=$_POST['site'];

			//$email_info[0]['email']=$_POST['email'];

			$img = CUploadedFile::getInstance($info[0],'avatar');
			//$upload = Yii::getPathOfAlias('istud').Yii::app()->controller->module->uploadPath;
			if ($img != null)
			{
				$img->saveAs("images/users/avatars/".$img);
			}
			$info[0]['avatar']=$img;

			if ($info[0]->save())
			{
				$this->redirect('userpage');
			}
			else
			{
				$this->redirect('edit');
			}
		}
		else
		{
			$this->redirect('userpage');
		}
	}

	public function actionEdit()
	{
		$user=new User();
		$update=User::model()->with('user_info')->findByPk(Yii::app()->user->getId());
		$this->render('changemyinfo',array('update'=>$update));
	}

        function actionAddfriend()
        {
            if(isset($_GET['id']))
            {
                $user=new User();
                $user_friend=new user_friend();
                $user_friend->user_id_f=Yii::app()->user->getId();
                $user_friend->id_user_friend=$_GET['id'];
                $user_friend->save();
                $this->redirect('/user/userpage');

            }

        }

        function actionOrder()
        {
           if(isset ($_GET['id']))
           {
               $order=order::model()->findAll('user_id=:user_id',array(':user_id'=>$_GET['id']));
               $this->render('user_order',array('order'=>$order));
           }
        }

        function actionDel_order()
        {
            if(isset ($_GET['id']))
           {
               $order=order::model()->deleteByPk($_GET['id']);
              // $this->render('user_order',array('order'=>$order));
               $user=new User();

               $user=user::model()->findByPk(Yii::app()->user->getId());
                $count_zak=$user->user_info->zak;
                $user->user_info->zak=$count_zak-1;
                $user->user_info->save();
                $this->redirect('/user/userpage/');
           }
        }

           function actionSell()
        {
           if(isset ($_GET['id']))
           {
               $sell=sell::model()->findAll('id_user=:id_user',array(':id_user'=>$_GET['id']));
               $this->render('user_sell',array('sell'=>$sell));
           }
        }

}
?>
